﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vibz.TeamAssignment.Common.Entity
{
    public class TaskView
    {
        //public const string ParentID = "Parent ID";
        //public const string TaskID = "Task ID";
        //public const string Title = "Title";
        //public const string Description = "Description";
        //public const string DueDate = "DueDate";
        //public const string EstimatedDuration = "Estimated Effort";
        //public const string ActualDuration = "Actual Effort";
        //public const string CreatedBy = "Created By";
        //public const string TaskStatus = "TaskStatus";

        //public const string ID = "ID";
        //public const string AssignBy = "AssignBy";
        //public const string AssignTo = "AssignTo";
        //public const string AssignDate = "AssignDate";
        //public const string IsRequest = "IsRequest";
        //public const string AssignComment = "Assign Comment";
        //public const string AssignStatus = "Assign Status";

        //public const string TaskGroupID = "TaskGroupID";
        //public const string TaskGroup = "Task Group";

        public Task Task
        {
            get;
            set;
        }
        public TaskGroup Group
        {
            get;
            set;
        }
        public TaskAssignment LatestAssignment
        {
            get;
            set;
        }

        public TaskView(Task task, TaskGroup tGroup, TaskAssignment tAss)
        {
            Task = task;
            Group = tGroup;
            LatestAssignment = tAss;
        }
        public TaskView Copy
        {
            get
            {
                return new TaskView(this.Task, this.Group, this.LatestAssignment);
            }
        }
        public DataRow Serialize(ref DataTable returnTable)
        {
            DataRow row = returnTable.NewRow();
            row.ItemArray = new object[] { Task.ParentID, Task.TaskID, Task.Title, Task.TaskDescription, 
                Task.DueDate, Task.EstimatedDuration, Task.ActualDuration, Task.CreatedBy, Task.Status,
                LatestAssignment.ID, LatestAssignment.AssignBy, LatestAssignment.AssignedTo, 
                LatestAssignment.AssignRequestDate, LatestAssignment.IsRequest, 
                LatestAssignment.AssignComment, LatestAssignment.AssignStatus, Group.ID, Group.Title
            };

            return row;
        }
        public static TaskView DeSerialize(DataRowView dr)
        {
            Task task = new Task(
                    int.Parse(dr[ViewColumns.ParentID.ToString()].ToString()), 
                        dr[ViewColumns.Title.ToString()].ToString(), 
                        dr[ViewColumns.Description.ToString()].ToString(),
                        DateTime.Parse(dr[ViewColumns.DueDate.ToString()].ToString()),
                        double.Parse(dr[ViewColumns.EstimatedDuration.ToString()].ToString()),
                        int.Parse(dr[ViewColumns.TaskGroupID.ToString()].ToString()), 
                        int.Parse(dr[ViewColumns.TaskID.ToString()].ToString()), 
                        (Vibz.TeamAssignment.Common.Entity.TaskStatus)Enum.Parse(
                        typeof(Vibz.TeamAssignment.Common.Entity.TaskStatus),
                        dr[ViewColumns.TaskStatus.ToString()].ToString()),
                        double.Parse(dr[ViewColumns.ActualDuration.ToString()].ToString()),
                        dr[ViewColumns.CreatedBy.ToString()].ToString());

            TaskGroup tGroup=new TaskGroup(
                    int.Parse(dr[ViewColumns.TaskGroupID.ToString()].ToString()), 
                    dr[ViewColumns.TaskGroup.ToString()].ToString());

            TaskAssignment tAss = new TaskAssignment(
                        int.Parse(dr[ViewColumns.TaskID.ToString()].ToString()),
                        dr[ViewColumns.AssignTo.ToString()].ToString(),
                        dr[ViewColumns.AssignBy.ToString()].ToString(),
                        bool.Parse(dr[ViewColumns.IsRequest.ToString()].ToString()),
                        DateTime.Parse(dr[ViewColumns.AssignDate.ToString()].ToString()),
                        dr[ViewColumns.AssignComment.ToString()].ToString(),
                     (Vibz.TeamAssignment.Common.Entity.AssignStatus)Enum.Parse(
                        typeof(Vibz.TeamAssignment.Common.Entity.AssignStatus),
                        dr[ViewColumns.AssignStatus.ToString()].ToString()),
                        int.Parse(dr[ViewColumns.ID.ToString()].ToString()));

            return new TaskView(task, tGroup, tAss);
        }
        public static DataTable Serialize(List<TaskView> tViewList)
        {
            DataTable returnTable = new DataTable("TaskView");

            returnTable.Columns.Add(new DataColumn(ViewColumns.ParentID.ToString(), typeof(int)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.TaskID.ToString(), typeof(int)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.Title.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.Description.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.DueDate.ToString(), typeof(DateTime)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.EstimatedDuration.ToString(), typeof(double)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.ActualDuration.ToString(), typeof(double)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.CreatedBy.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.TaskStatus.ToString(), typeof(TaskStatus)));

            returnTable.Columns.Add(new DataColumn(ViewColumns.ID.ToString(), typeof(int)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.AssignBy.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.AssignTo.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.AssignDate.ToString(), typeof(DateTime)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.IsRequest.ToString(), typeof(bool)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.AssignComment.ToString(), typeof(string)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.AssignStatus.ToString(), typeof(AssignStatus)));

            returnTable.Columns.Add(new DataColumn(ViewColumns.TaskGroupID.ToString(), typeof(int)));
            returnTable.Columns.Add(new DataColumn(ViewColumns.TaskGroup.ToString(), typeof(string)));

            foreach(TaskView tView in tViewList)
            {
                returnTable.Rows.Add(tView.Serialize(ref returnTable));
            }
            return returnTable;
        }
        public static Dictionary<TaskView, DeltaChange> GetDeltaUpdate(List<TaskView> oldView, List<TaskView> newView, string userID)
        {
            if(oldView == null || newView == null)
                return null;
            Dictionary<TaskView, DeltaChange> retValue=new Dictionary<TaskView,DeltaChange>();
            foreach(TaskView tv in newView)
            {
                if(oldView.Where(v => v.Task.TaskID == tv.Task.TaskID).ToList().Count == 0 
                    && tv.LatestAssignment.AssignBy!= userID)
                    retValue.Add(tv.Copy, DeltaChange.New);
            }
            return retValue;
        }
    }
    public enum DeltaChange
    {
        New,
        Update,
        Removed
    }
}
